package com.lemon.rpcframe.dao.behavior;

import java.util.Date;
import java.util.List;

import com.lemon.rpcframe.annotation.ServiceBehavior;
import com.lemon.rpcframe.dao.DBHelper;
import com.lemon.rpcframe.dao.Total;
import com.lemon.rpcframe.dao.contract.ITaskExecutingService;
import com.lemon.rpcframe.dao.entity.TaskExecutingBean;
import com.lemon.rpcframe.util.DateUtil;
import com.lemon.rpcframe.util.StringUtils;

@ServiceBehavior
public class TaskExecutingServiceImpl implements ITaskExecutingService {

    public static final int DEFAULT_PAGESIZE = 30;

    @Override
    public long insert(TaskExecutingBean entity) throws Exception {
        if (null == entity) {
            return 0l;
        }
        long id = 0l;
        Object obj = DBHelper.getInstance().getdaoHelper().sql.insert(entity);
        if (null != obj) {
            id = (long) obj;
        }
        return id;
    }

    @Override
    public void deleteByJobID(long jobid) throws Exception {
        if (jobid < 1) {
            return;
        }
        String where = String.format(" jobid = %d limit 1", jobid);
        DBHelper.getInstance().getdaoHelper().sql.deleteByCustom(TaskExecutingBean.class, where);
    }

    @SuppressWarnings("unchecked")
    @Override
    public TaskExecutingBean loadByJobID(long jobid) throws Exception {
        if (jobid < 1) {
            return null;
        }
        String where = String.format(" jobid = %d limit 1", jobid);
        List<TaskExecutingBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(TaskExecutingBean.class, "", where, "");
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskExecutingBean> loadByTaskTracker(String taskTrackerIdentity) throws Exception {
        if (StringUtils.isNullOrEmpty(taskTrackerIdentity)) {
            return null;
        }
        String where = String.format(" taskIdentity = '%s' limit 100 ", taskTrackerIdentity);

        List<TaskExecutingBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(TaskExecutingBean.class, "", where, "");

        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskExecutingBean> loadByCreateDate(Date createDate) throws Exception {
        if (createDate == null) {
            return null;
        }
        String where = String.format(" createDate < '%s' limit 100 ", DateUtil.fullsdf.format(createDate));
        List<TaskExecutingBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(TaskExecutingBean.class, "", where, "");

        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TaskExecutingBean> loadByPage(int pageSize, int page, Total<Integer> total) throws Exception {
        total.setTotal(countAll());
        List<TaskExecutingBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByPage(TaskExecutingBean.class, "", "", page, pageSize, " triggerDate desc");
        return list;
    }

    @Override
    public int countAll() throws Exception {
        int cnt = DBHelper.getInstance().getdaoHelper().sql.getCount(TaskExecutingBean.class, "");
        return cnt;
    }
}
